package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.UserBean;
import com.project.mapper.UserMapper;
import com.project.service.IUserService;
import com.project.util.codingUtils.NumTranceUtil;
import com.project.util.codingUtils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户端的用户表 服务实现类
 * </p>
 *
 * @author lhy
 * @since 2021-10-05
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserBean findByOpenid(String openid) {
        UserBean userBean = userMapper.selectOne(new QueryWrapper<UserBean>().eq("openid", openid));
        if (userBean == null) {
            userBean = new UserBean();
            userBean.setOpenid(openid);
            SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
            userBean.setUserCode(NumTranceUtil.digits32(idWorker.nextId()));
            userMapper.insert(userBean);
        }
        return userBean;
    }

    @Override
    public IPage<UserBean> findByItem(int pn, int pageSize, String userCode, String realName, String gentle, String phone) {
        Page<UserBean> page = new Page<>(pn, pageSize);
        IPage<UserBean> pages = userMapper.findByItem(page, userCode, realName, gentle, phone);
        return pages;
    }

    @Override
    public void registerUser(String userCode, String realName, String phone, String gentle) {
        userMapper.registerUser(userCode, realName, phone, gentle);
    }

    @Override
    public UserBean updateRiderStatus(String userCode) {
        userMapper.updateRiderStatus(userCode);
        return userMapper.selectOne(new QueryWrapper<UserBean>().eq("userCode", userCode));
    }

    @Override
    public UserBean updateShopStatus(String userCode) {
        userMapper.updateShopStatus(userCode);
        return userMapper.selectOne(new QueryWrapper<UserBean>().eq("userCode", userCode));
    }
}
